﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class SectionLearnBLL : CommonBLL<SectionLearnInfo, SectionLearnDAL>
    {
        public static bool Learn(int sid, int uid)
        {
            using (var dal = new SectionLearnDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Sid == sid && o.Uid == uid);
                if (re == null)
                {
                    dal.Add(new SectionLearnInfo { Uid = uid, Sid = sid, IsLearned = true });
                }
                return true;
            }
        }

        public static int GetLearnCount(int uid)
        {
            return GetList().Count(o => o.Uid == uid);
        }

        public static SectionLearnInfo Get(int sid, int uid)
        {
            using (var dal = new SectionLearnDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Sid == sid && o.Uid == uid);
                return re;
            }
        }

        public static SectionLearnInfo Get(int? id)
        {
            using (var dal = new SectionLearnDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new SectionLearnDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
